package im.zego.zimexample.ui;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.google.android.material.tabs.TabLayout;

import im.zego.zim.callback.ZIMRoomCreatedCallback;
import im.zego.zim.callback.ZIMRoomJoinedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMRoomFullInfo;
import im.zego.zim.enums.ZIMErrorCode;
import im.zego.zimexample.MyApplication;
import im.zego.zimexample.MyBasicActivity;
import im.zego.zimexample.R;
import im.zego.zimexample.SDKManager;
import im.zego.zimexample.ui.peer.ZIMPeerSessionActivity;
import im.zego.zimexample.ui.room.ZIMRoomSessionActivity;
import im.zego.zimexample.util.CustomDialog;
import im.zego.zimexample.util.GifView;

public class StartChatActivity extends MyBasicActivity {
    private GifView gifView_1;
    private GifView gifView_2;



    public static void actionStart(Activity mainActivity, String userID, String userName) {
        Intent intent = new Intent(mainActivity, StartChatActivity.class);
        intent.putExtra("userID", userID);
        intent.putExtra("userName", userName);

        mainActivity.startActivity(intent);
    }

    String userID, userName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉窗口标题
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏部分（电池、信号等）  全屏显示
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //必须放在这个ContentView之前
        setContentView(R.layout.activity_start_chat);
        userID = getIntent().getStringExtra("userID");
        userName = getIntent().getStringExtra("userName");
        com.google.android.material.tabs.TabLayout mTabLayout = findViewById(R.id.tab_layout);
        // 添加 tab item
        mTabLayout.addTab(mTabLayout.newTab().setText("Peer"));
        mTabLayout.addTab(mTabLayout.newTab().setText("Room"));

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.view.getTab().getText().equals("Peer")) {
                    findViewById(R.id.room).setVisibility(View.GONE);
                    findViewById(R.id.peer).setVisibility(View.VISIBLE);
                    //更新gif
                    gifView_1.setVisibility(View.GONE);
                    gifView_2.setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.room).setVisibility(View.VISIBLE);
                    findViewById(R.id.peer).setVisibility(View.GONE);
                    //更新gif
                    gifView_1.setVisibility(View.VISIBLE);
                    gifView_2.setVisibility(View.GONE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        initGifView();
    }

    private void initGifView() {
        // giftView
        gifView_1 = findViewById(R.id.gifView_1);
        gifView_1.setGifResource(R.raw.gif_3);    // 设置显示的Gif图片

        gifView_2 = findViewById(R.id.gifView_2);
        gifView_2.setGifResource(R.raw.gif_1);
       createGifView(gifView_1);
       createGifView(gifView_2);
       gifView_1.setVisibility(View.GONE);
    }

    private void createGifView(GifView gifView_1) {
        int width = gifView_1.getGifWidth();
        int height = gifView_1.getGifHeight();
        int screenWidth = getScreenSize().width();
        int screenHeight = getScreenSize().height();
        if (width > 0 && height > 0) {
            float wScale = (float) screenWidth / width;
            float hScale = (float) screenHeight / height;
            if (wScale < 1 || hScale < 1) {
                // 如果图片的宽或高大于屏幕的宽或高，则图片会自动缩小至全屏
            } else if (wScale <= hScale) {
                // 宽度全屏
                gifView_1.setScaleX(wScale);
                gifView_1.setScaleY(wScale);
            } else {
                // 高度全屏
                gifView_1.setScaleX(hScale);
                gifView_1.setScaleY(hScale);
            }
        }
    }
    //utils
    /**
     * 得到屏幕的尺寸
     *
     * @return 包含屏幕尺寸的Rect对象
     */
    private Rect getScreenSize() {
        Rect localRect = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(localRect);
        return localRect;
    }
    @Override
    protected void onStart() {
        super.onStart();
        SDKManager.sharedInstance().showDebugLog(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKManager.sharedInstance().closeDebugLog(this);
    }

    public void back(View view) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void finish() {
        super.finish();
        SDKManager.sharedInstance().logout();
    }

    public void chat(View view) {
        EditText editText = findViewById(R.id.user_id);
        if (!"".equals(editText.getText().toString().replace(" ", ""))) {
            ZIMPeerSessionActivity.actionStart(this, editText.getText().toString(), userID, userName);
        } else {
            Toast.makeText(this, "Please enter a valid userID", Toast.LENGTH_LONG).show();
        }
    }

    public void create(View view) {
        EditText roomIDEditText = findViewById(R.id.roomID);
        EditText roomNameEditText = findViewById(R.id.roomName);

        SDKManager.sharedInstance().createRoom(roomIDEditText.getText().toString(), roomNameEditText.getText().toString(), new ZIMRoomCreatedCallback() {

            @Override
            public void onRoomCreated(ZIMRoomFullInfo zimRoomFullInfo, ZIMError error) {
                CustomDialog.createDialog(StartChatActivity.this).cancel();
                if (error.code == ZIMErrorCode.SUCCESS) {
                    // 创建房间
                    ZIMRoomSessionActivity.actionStart(StartChatActivity.this, userID, roomIDEditText.getText().toString());
                } else {
                    Toast.makeText(StartChatActivity.this, "Failed to create room error info :" + error.code.name(), Toast.LENGTH_LONG).show();
                }
            }
        });

    }

    public void join(View view) {
        EditText roomIDEditText = findViewById(R.id.roomID);
        EditText roomNameEditText = findViewById(R.id.roomName);

        SDKManager.sharedInstance().joinRoom(roomIDEditText.getText().toString(), new ZIMRoomJoinedCallback() {
            @Override
            public void onRoomJoined(ZIMRoomFullInfo zimRoomFullInfo, ZIMError zimError) {
                CustomDialog.createDialog(StartChatActivity.this).cancel();
                if (zimError.code == ZIMErrorCode.SUCCESS) {
                    // 加入房间
                    ZIMRoomSessionActivity.actionStart(StartChatActivity.this, userID, roomIDEditText.getText().toString());
                } else {
                    Toast.makeText(StartChatActivity.this, "Failed to join the room error info:" + zimError.code.name(), Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}
